package 案例;

//复制文件
import java.io.*;
import java.util.Scanner;

public class Demo_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要复制的源文件：");
        String srcPath = scanner.next();
        System.out.println("输入要复制的目标文件：");
        String rootPath = scanner.next();

        //判断位置的合法性
        File srcFile = new File(srcPath);
        if(!srcFile.isFile()){
            System.out.println("源文件地址有误，重新输入");
            return;
        }
        File rootFile = new File(rootPath);
        if(!rootFile.getParentFile().isDirectory()){
            System.out.println("目的地址的父目录不存在");
            return;
        }

        //进行复制
        try(InputStream inputStream = new FileInputStream(srcFile);
                OutputStream outputStream = new FileOutputStream(rootFile)){
            while(true){
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n==-1){
                    break;
                }
                outputStream.write(buffer,0,n);
            }
        }  catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
